package StudyCode.summary.mycollection;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ConvertsAListToANewCopyOfArrayBasedOnTheStartIndexAndEndIndex {
	public static void main(String[] argv) {
		List list = java.util.Arrays.asList("科技与人文", "www.tah1986.com");
		System.out.println(java.util.Arrays.toString(toArray(list)));
	}

	@SuppressWarnings({"unchecked", "cast"})
	public static final <T> T[] toArray(List<T> list, int startIndex,
	                                    int endIndex) {
		if (isEmpty(list)) {
			return (T[]) list.toArray();
		}
		List<T> subList = list.subList(startIndex, endIndex);
		return (T[]) subList.toArray((T[]) java.lang.reflect.Array
				.newInstance(list.get(0).getClass(), subList.size()));
	}

	public static final <T> T[] toArray(List<T> list) {
		return toArray(list, 0, list.size());
	}

	public static <T> boolean isEmpty(Collection<T> collection) {
		return collection == null || collection.isEmpty();
	}

	public static <T, U> boolean isEmpty(Map<T, U> map) {
		return map == null || map.isEmpty();
	}

	public static int size(Collection<? extends Object> collection) {
		if (collection == null) {
			return 0;
		}
		return collection.size();
	}
}
